﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

using LowLevelGraphics;
using LowLevelGraphics.Filter;
using ImageRecognition2.Forms;

namespace ImageRecognition2.Action
{
    /// <summary>
    /// 
    /// </summary>
    public class ActionAdjustBrightness : AbstractCommand
    {
        protected BaseImageFilter m_Filter = new PerPixelFilter(new PerPixelFilter.PerPixelFilterDelegate(ColorPixelFilter));
        protected static int m_nR = 0;
        protected static int m_nG = 0;
        protected static int m_nB = 0;

        FormAdjustBrightness m_FormFilter = null;

        /// <summary>
        /// 
        /// </summary>
        public ActionAdjustBrightness()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Run(object sender, EventArgs e)
        {
            m_FormFilter = new FormAdjustBrightness((Bitmap)MainFormSingleton.ActivePictureBox.BackgroundImage.Clone());
            m_FormFilter.trackbBrightnessR.Scroll += new EventHandler(trackbBrightness_Scroll);
            m_FormFilter.trackbBrightnessG.Scroll += new EventHandler(trackbBrightness_Scroll);
            m_FormFilter.trackbBrightnessB.Scroll += new EventHandler(trackbBrightness_Scroll);

            if (m_FormFilter.ShowDialog() == DialogResult.OK)
            {
                Bitmap bitmap = (Bitmap)MainFormSingleton.ActivePictureBox.BackgroundImage;
                UnsafeBitmap unsafeBitmap = m_Filter.Execute(bitmap);
                unsafeBitmap.Dispose();
            }
        }

        void trackbBrightness_Scroll(object sender, EventArgs e)
        {
            Execute(m_FormFilter);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="formFilter"></param>
        private void Execute(FormAdjustBrightness formFilter)
        {
            m_nR = formFilter.trackbBrightnessR.Value;
            m_nG = formFilter.trackbBrightnessG.Value;
            m_nB = formFilter.trackbBrightnessB.Value;

            Bitmap formFilterpictureBox1BackgroundImageClone = (Bitmap)formFilter.imgOriginal.Clone();
            BaseImageFilter baseImageFilter = null;
            baseImageFilter = m_Filter;
            baseImageFilter.ProcessingMode = PROCESSING_MODE.Color;
            baseImageFilter.Execute(formFilterpictureBox1BackgroundImageClone).Dispose();
            formFilter.pictureBox1.BackgroundImage = formFilterpictureBox1BackgroundImageClone;
            formFilter.pictureBox1.Invalidate();
        }

        /// <summary>
        /// Adjust colors
        /// </summary>
        /// <param name="_color"></param>
        /// <returns></returns>
        private static Color ColorPixelFilter(Color _color)
        {
            return Color.FromArgb(_color.R + m_nR, _color.G + m_nG, _color.B + m_nB);
        }

        /// <summary>
        /// Clone this instance
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return base.Clone();
        }
    }
}
